﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;

namespace ProjectQuaDep
{
    public partial class ListNews : System.Web.UI.Page
    {
        protected string urlZone = "";
        protected string titleZone = "";
        int PageIndex = lib.PageIndex;
        int PageSize = 10;
        private string sFormatNews = @"<li><a href=""{0}""><img src=""{1}"" /></a><h3><a href=""{0}"">{2}</a></h3><p>{3}</p></li>";
        protected void Page_Load(object sender, EventArgs e)
        {
            int zoneID = BO.lib.ZoneID;
            urlZone = BO.ReWriteUrl.BuildLink.BuildLinkZoneNews(zoneID.ToString());
            DataTable dtZone = BO.News.ZoneBO.GetDetailZoneBO(zoneID.ToString());
            if (dtZone != null && dtZone.Rows.Count > 0)
            {
                DataRow dr = dtZone.Rows[0];
                titleZone = dr[BO.Const.tbZoneNews.title].ToString();
                //
                //set meta
                string Trang = "";
                if (PageIndex > 1)
                {
                    Trang = " | Trang " + PageIndex;
                }
                Page.Title = dr[Const.tbZoneNews.title].ToString() + Trang;
                string hDesc = dr[Const.tbZoneNews.title].ToString() + Trang;
                string hKey = dr[Const.tbZoneNews.title].ToString();
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Description.Id, BAL.Const.MetaAttributes.Description.Name, hDesc);
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.Keywords.Id, BAL.Const.MetaAttributes.Keywords.Name, hKey);
                BAL.Utils.AddMeta(this.Page, BAL.Const.MetaAttributes.NewKeywords.Id, BAL.Const.MetaAttributes.NewKeywords.Name, hKey);
            }
            //show tin ra nao
            DataTable dt = new DataTable();
            dt = BO.News.NewsBO.GetListNewsByZone(PageSize.ToString(), PageIndex.ToString(), zoneID.ToString());
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    string title = dr[BO.Const.tbNews.Title].ToString();
                    string sapo = dr[BO.Const.tbNews.Sapo].ToString();
                    string NewsID = dr[BO.Const.tbNews.id].ToString();
                    string url = BO.ReWriteUrl.BuildLink.BuildLinkNews(NewsID);
                    string Images = BO.Utils.CropImage(156, 130, dr[BO.Const.tbNews.Avatar].ToString());
                    ltListNews.Text += string.Format(sFormatNews, url, Images, title, sapo);
                }
                //phan trang
                //set phan trang
                string linkPage = "";
                linkPage = urlZone.Replace(".htm","") + "/";
                int totalPage = int.Parse(BO.News.NewsBO.CountGetListNewsByZoneBO(zoneID.ToString()).Rows[0][Const.tbNews.TongBanGhi].ToString());
                this.PaddingNews1.PageSize = PageSize;
                int SumPage = totalPage % PageSize == 0 ? totalPage / PageSize : (totalPage / PageSize) + 1;
                this.PaddingNews1.SumPage = SumPage;
                if (this.PaddingNews1.SumPage <= PageSize)
                {
                    this.PaddingNews1.Visible = false;
                }
                this.PaddingNews1.LinkSite = linkPage;
                this.PaddingNews1.Padding(PageIndex);
            }
            else
            {
                ltListNews.Text = "</br><b>Hiện chuyên mục này chưa có dữ liệu bạn hãy ghé thăm những chuyên mục khác nhé</b>";
            }
        }
    }
}